home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.software-eng,comp.unix.solaris,comp.unix.programmer,comp.windows.x,comp.windows.x.motif,comp.lang.c,comp.lang.c++,comp.lang.tcl,comp.object,comp.lang.fortran
- Path: mozart.slac.stanford.edu!user
- From: ljm@slac.stanford.edu (Leonard J. Moss)
- Subject: Re: structures as formal parameters
- Message-ID: <ljm-2202961835270001@mozart.slac.stanford.edu>
- Sender: news@unixhub.SLAC.Stanford.EDU
- Organization: Stanford Linear Accelerator Center
- X-Newsreader: Yet Another NewsWatcher 2.2.0b4
- References: <4fut4m$h5t@Dortmund.Germany.EU.net> <31267EED.4874066A@holisticmath.com> <4g6102$lql@portal.gmu.edu>
- Distribution: inet
- Date: Fri, 23 Feb 1996 02:35:27 GMT
-
- In article <4g6102$lql@portal.gmu.edu>, jscheibl@osf1.gmu.edu (Jack W
- Scheible) wrote:
-
- >I am trying to pass structures to subroutines. My problem is that,
- >when I try to write the interface, it will not recognize previously
- >declared types. I can put the type definition into the interface, but
- >then it is not recognized outside the interface.
- >
- > PROGRAM JUNK
- >
- > TYPE CARTESIAN
- > REAL*8 X
- > REAL*8 Y
- > END TYPE CARTESIAN
- >
- > INTERFACE
- > SUBROUTINE CART_TO_POLAR ( XY, RTHETA )
- > TYPE POLAR
- > REAL*8 R
- > REAL*8 THETA
- > END TYPE POLAR
- > TYPE (CARTESIAN), INTENT(IN) :: XY ! CARTESIAN Not Declared
- > TYPE (POLAR), INTENT(OUT):: RTHETA ! Just fine
- > END SUBROUTINE CART_TO_POLAR
- > END INTERFACE
- >
- > TYPE (CARTESIAN) XY ! Just fine
- > TYPE (POLAR) RTHETA ! POLAR Not Declared
- >
- > END
- >
- >Is there any way to use structures in an interface without declaring
- >the structures twice?
-
- Yes -- put the type definitions in a module; for example:
-
- MODULE COORD_TYPES
- TYPE CARTESIAN
- REAL*8 X
- REAL*8 Y
- END TYPE CARTESIAN
- TYPE POLAR
- REAL*8 R
- REAL*8 THETA
- END TYPE POLAR
- END MODULE COORD_TYPES
-
- PROGRAM JUNK
- USE COORD_TYPES
- INTERFACE
- SUBROUTINE CART_TO_POLAR ( XY, RTHETA )
- USE COORD_TYPES
- TYPE (CARTESIAN), INTENT(IN) :: XY ! CARTESIAN Not Declared
- TYPE (POLAR), INTENT(OUT):: RTHETA ! Just fine
- END SUBROUTINE CART_TO_POLAR
- END INTERFACE
- TYPE (CARTESIAN) XY ! Just fine
- TYPE (POLAR) RTHETA ! POLAR Not Declared
- ! Program code...
- END
-
- Note that you must also USE the module in the INTERFACE block (even though
- there's already a USE in your main program). Remember that the INTERFACE
- block is intended to describe to the caller all the characteristics of an
- independently compiled procedure. Since the latter doesn't have a host
- from which to inherit things like type definitions or IMPLICIT mappings,
- the INTERFACE block can't inherit from it's host either.
-
- Lest Loren take me to task, let me point out that a much better solution
- is to put _all_ your procedures and type definitions in modules and
- dispense with INTERFACE blocks altogether (except for overloading). For
- example,
-
- MODULE COORD_SYSTEMS
- TYPE CARTESIAN
- REAL*8 X
- REAL*8 Y
- END TYPE CARTESIAN
- TYPE POLAR
- REAL*8 R
- REAL*8 THETA
- END TYPE POLAR
- CONTAINS
- SUBROUTINE CART_TO_POLAR( XY, RTHETA )
- TYPE (CARTESIAN), INTENT(IN) :: XY
- TYPE (POLAR), INTENT(OUT):: RTHETA
- ! Code to do conversion...
- END SUBROUTINE CART_TO_POLAR
- SUBROUTINE POLAR_TO_CART( RTHETA, XY )
- TYPE (POLAR), INTENT(OUT):: RTHETA
- TYPE (CARTESIAN), INTENT(IN) :: XY
- ! Code to do conversion...
- END SUBROUTINE POLAR_TO_CART
- END MODULE COORD_SYSTEMS
-
- PROGRAM JUNK
- USE COORD_SYSTEMS
- TYPE (CARTESIAN) XY
- TYPE (POLAR) RTHETA
- ! Program code...
- END
-
- Notice that the subroutine definitions now _can_ inherit the type
- definitions from their host, and the caller can bring in both type
- definitions and procedure interfaces with a simple USE statement.
-
- A few other points:
-
- o REAL*8 is non-standard (I assume it's an accepted extension on your
- compiler)
- o If for some reason you don't choose to put TYPE definitions in a module,
- but instead duplicate the type definitions where needed, the standard
- requires that you put the SEQUENCE keyword in each type definition.
- o If for some reason you don't choose to put the actual code for your
- procedures in the module (e.g., it's proprietary and you don't want to
- distrubute the source), you can still put the interface blocks in a
- module so the user doesn't need to embed difficult to maintain INTERFACE
- blocks in his/her code. However, in this case you must put the type
- definitions and interface blocks in separate modules, since INTERFACE
- blocks cannot (as we've seen above) inherit the type definitions from
- their host, and also cannot contain a USE for the MODULE in which they
- occur.
- o If you eventually want to overload assignment with conversion
- subroutines like those above, the order of the dummy arguments must be
- reversed.
-
- --
- Leonard J. Moss <ljm@slac.stanford.edu> | My views don't necessarily
- Stanford Linear Accelerator Center | reflect those of SLAC,
- MS 97; P.O. Box 4349; Stanford, CA 94309 | Stanford or the DOE
-